package cn.com.gome.sn.web.util;

import cn.com.gome.frame.logger.HLogger;
import cn.com.gome.frame.logger.LoggerManager;

import java.io.UnsupportedEncodingException;
import java.math.BigInteger;

/**
 * 异或进行加密解密算法
 * @author lijiaqi
 *
 */
public class EncrUtil {

	private static HLogger logger = LoggerManager.getLoger("EncrUtil");

	private static final int RADIX = 16;
	private static final String SEED = "0933910847463829232312312";

	private static final String defaultCharset = "UTF-8";

	
	//加密
	public static final String encrypt(String password) {
		if (password == null)
			return "";
		if (password.length() == 0)
			return "";

		BigInteger bi_passwd = null;
		try {
			bi_passwd = new BigInteger(password.getBytes(defaultCharset));
		} catch (UnsupportedEncodingException e) {
			logger.error("加密异常",e);
			return "";
		}

		BigInteger bi_r0 = new BigInteger(SEED);
		BigInteger bi_r1 = bi_r0.xor(bi_passwd);

		return bi_r1.toString(RADIX);
	}

	//解密
	public static final String decrypt(String encrypted) {
		if (encrypted == null)
			return "";
		if (encrypted.length() == 0)
			return "";

		BigInteger bi_confuse = new BigInteger(SEED);

		try {
			BigInteger bi_r1 = new BigInteger(encrypted, RADIX);
			BigInteger bi_r0 = bi_r1.xor(bi_confuse);

			return new String(bi_r0.toByteArray(),defaultCharset);
		} catch (Exception e) {
			logger.error("解密异常",e);
			return "";
		}
	}
}